﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Avatar.MembershipService;
using Avatar.Management;
public partial class UserControl_Register : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PanelRegister.Visible = true;
            PanelSuccess.Visible = false;

            var _siteInformationConfig = SiteInformationManager.GetSiteInformation();
            if (_siteInformationConfig != null)
            {
                lbl_SiteName.Text = _siteInformationConfig.SiteName;
                lbl_EmailII.Text = _siteInformationConfig.Email;
            }
        }
        var _typeID = Request.QueryString["type"] != null ? Convert.ToInt32(Request.QueryString["type"].ToString()) : 0;
        txt_RoleName.Value = _typeID == 0 ? "个人用户" : "企业用户";
    }
    protected void btn_Register_Click(object sender, EventArgs e)
    {
        try
        {
            if (!accept.Checked) throw new Exception("您还没有阅读且同意‘用户协议’与‘隐私政策’。");

            LoginManager.Register(txt_Pwd.Text, txt_Email.Text, txt_VCode.Text, txt_RoleName.Value);
            
            PanelSuccess.Visible = true;
            PanelRegister.Visible = false;
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "用户注册失败:" + _e.Message;
        }
    }

    protected void CheckEmail_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txt_Email.Text)) throw new Exception("Email 不能为空！");
            if (!string.IsNullOrEmpty(Membership.GetUserNameByEmail(txt_Email.Text)))
            {
                throw new Exception("该Email已被占用.");
            }
            else
            {
                lbl_Email.Text = "恭喜,可以使用!";
            }
        }
        catch (Exception _e)
        {
            lbl_Email.Text = "错误：" + _e.Message;
        }
    }
    protected void btn_Success_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/");
    }
}
